package igou.togogo.service.impl;

import igou.togogo.common.service.impl.BaseServiceImpl;
import igou.togogo.domain.ProductDetail;
import igou.togogo.domain.ProductResult;
import igou.togogo.pojo.EgouResult;
import igou.togogo.service.IProductService;
import igou.togogo.utils.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ProductServiceImpl extends BaseServiceImpl implements IProductService {

    @Override
    public ProductResult getProductByBrandId(Integer brandId, Integer page, Integer rows) {
        Map<String ,String> parms = new HashMap<>();
        parms.put("brandId",brandId.toString());
        parms.put("page",page.toString());
        parms.put("rows",rows.toString());
        String json = HttpClientUtil.doGet(REST_BASE_URL + PRODUCT_LIST_URL, parms);
        if (StringUtils.isNotBlank(json)){
            EgouResult result = EgouResult.formatToPojo(json, ProductResult.class);
            return (ProductResult) result.getData();
        }
        return null;
    }

    @Override
    public ProductDetail getProductDetail(Integer id, String colorId) {
        String url = REST_BASE_URL + PRODUCT_DETAIL_URL + id.toString() +"?color_id=" + colorId;
        String json = HttpClientUtil.doGet(url);
        if (StringUtils.isNotBlank(json)) {
            EgouResult result = EgouResult.formatToPojo(json, ProductDetail.class);
            return (ProductDetail) result.getData();
        }
        return null;
    }
}
